﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace CM
{
    class Bukkit
    {

        string Appdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\CM";

        public bool Update(string oldpath, string newpath, string serverpath)
        {
            WebClient Webclient = new WebClient();
            Webclient.DownloadFileAsync(new Uri("ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar"),
            Appdata + @"\craftbukkit.jar");
            try
            {
                File.Delete(serverpath + @"\craftbukkit.jar");
                File.Move(Appdata + @"\craftbukkit.jar", serverpath + @"\craftbukkit.jar");
                return true;
            }
            catch
            {
                return false;
            }
        }

        public int IsBukkitUpdated(string path)
        {
            try
            {
                WebClient Webclient = new WebClient();
                Webclient.DownloadFileAsync(new Uri("ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar"),
                Appdata + @"\craftbukkit.jar");
                string PcFile, InternetFile;
                StreamReader Reader1 = new StreamReader(Appdata + @"\craftbukkit.jar");
                StreamReader Reader2 = new StreamReader(path);
                PcFile = Reader2.ReadToEnd();
                InternetFile = Reader1.ReadToEnd();
                if (PcFile == InternetFile)
                {
                    return 0;
                }
                else
                {
                    return 1;
                }
            }
            catch
            {
                return 2;
            }
        }

        public bool Download(string serverpath)
        {
            WebClient Webclient = new WebClient();
            Webclient.DownloadFileAsync(new Uri("ci.bukkit.org/job/dev-CraftBukkit/promotion/latest/Recommended/artifact/target/craftbukkit-0.0.1-SNAPSHOT.jar"),
            Appdata + @"\craftbukkit.jar");
            try
            {
                if (File.Exists(serverpath + @"\craftbukkit.jar"))
                {
                    File.Delete(serverpath + @"\craftbukkit.jar");
                }
                File.Move(Appdata + @"\craftbukkit.jar", serverpath + @"\craftbukkit.jar");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
